package com.hangwei.paperless.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.hangwei.paperless.App;
import com.hangwei.paperless.utils.KeyValue;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.UserInfo;
import com.twelve.all.utils.Tools;


/**
 * Created by 张龙臣 on 2020/11/12.
 */

public class SealAdapter extends RecyclerView.Adapter {

    private Context mContext;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext = viewGroup.getContext();
        return new RecyclerView.ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_seal, viewGroup, false)) {
        };
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
        RadioButton radioButton = viewHolder.itemView.findViewById(R.id.radioButton);
        ImageView ivSealIcon = viewHolder.itemView.findViewById(R.id.ivSealIcon);
        TextView tvSealNum = viewHolder.itemView.findViewById(R.id.tvSealNum);
        TextView rangeText = viewHolder.itemView.findViewById(R.id.rangeText);
        final UserInfo.StampBean stampBean = App.stamps.get(i);
        ivSealIcon.setImageDrawable(mContext.getResources().getDrawable(KeyValue.getSealPic(stampBean.stampTypeCode)));
        int sealType = KeyValue.getSealType(stampBean.stampTypeCode);
        if (sealType != -1) {
            if (sealType == 0) {
                tvSealNum.setVisibility(View.VISIBLE);
                tvSealNum.setText(Tools.checkedString(stampBean.stampCode));
                tvSealNum.setTextColor(mContext.getResources().getColor(R.color.blue1));
                rangeText.setVisibility(View.GONE);
            } else if (sealType == 1) {
                tvSealNum.setVisibility(View.VISIBLE);
                tvSealNum.setText(Tools.checkedString(stampBean.stampCode));
                rangeText.setVisibility(View.GONE);
                tvSealNum.setTextColor(mContext.getResources().getColor(R.color.red_1));
            } else {
                rangeText.setVisibility(View.VISIBLE);
                rangeText.setText(Tools.checkedString(stampBean.stampCode));
                tvSealNum.setVisibility(View.GONE);
                rangeText.setTextColor(mContext.getResources().getColor(R.color.red_1));
            }
        }
//        boolean haveChecked = false;
//        for (UserInfo.StampBean bean : App.stamps) {
//            if (bean.isChecked) {
//                haveChecked = true;
//                break;
//            }
//        }
//        if (!haveChecked)
//            App.stamps.get(0).isChecked = true;
        radioButton.setChecked(stampBean.isChecked);
        radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for (UserInfo.StampBean bean : App.stamps) {
                        bean.isChecked = false;
                    }
                }
                stampBean.isChecked = isChecked;
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged();
                    }
                });

            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean flag = stampBean.isChecked;
                for (UserInfo.StampBean bean : App.stamps) {
                    bean.isChecked = false;
                }
                stampBean.isChecked = !flag;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (App.stamps == null)
            return 0;
        return App.stamps.size();
    }
}
